package com.lyf.scm.plan.domain.repository;

import com.lyf.scm.plan.api.dto.RequirementConfirmationDTO;
import com.lyf.scm.plan.api.dto.RequirementPlanInfoDTO;
import com.lyf.scm.plan.insfrastructure.db.dataobject.RequirementConfirmationDO;
import com.lyf.scm.plan.insfrastructure.db.mapper.RequirementConfirmationMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 供应链确认量
 *
 * @author wangchlei
 * @date 2020/10/29
 */
@Service
public class RequirementConfirmationRepository {
    @Resource
    private RequirementConfirmationMapper requirementConfirmationMapper;

    /**
     * 根据批量条件：收货仓库，skucode，周起始日期     查找实际需要数据
     *
     * @param pageResult
     * @param startDate
     * @param endDate
     * @return {@link List<RequirementConfirmationDO>}
     **/
    public List<RequirementConfirmationDO> selectSupplyChainQty(List<RequirementPlanInfoDTO> pageResult, Date startDate, Date endDate) {
        return requirementConfirmationMapper.selectSupplyChainQty(pageResult, startDate, endDate);
    }

    public void updateConfirmQty(List<RequirementConfirmationDTO> requirementConfirmationDTOs, Long userId, String userName) {
        requirementConfirmationMapper.updateConfirmQty(requirementConfirmationDTOs,userId,userName);
    }

    public List<RequirementConfirmationDO> pullDataByWarehouse(String warehouseCode, Date startDate, Date endDate) {
        return requirementConfirmationMapper.pullDataByWarehouse(warehouseCode, startDate, endDate);
    }

    /**
     * 处理job结果
     *
     * @param inDO
     * @param upDO
     * @return void
     **/
    @Transactional(rollbackFor = Exception.class)
    public void handleResult(List<RequirementConfirmationDO> inDO, List<RequirementConfirmationDO> upDO) {
        if(!CollectionUtils.isEmpty(inDO)){
            requirementConfirmationMapper.batchInsert(inDO);
        }
        if(!CollectionUtils.isEmpty(upDO)){
            requirementConfirmationMapper.batchUpdate(upDO);
        }
    }
}
